package com.yhgarden.bbs.service.impl.business;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhgarden.bbs.entity.business.UserGroup;
import com.yhgarden.bbs.mapper.business.UserGroupMapper;
import com.yhgarden.bbs.model.enums.StatusCode;
import com.yhgarden.bbs.model.helper.Pager;
import com.yhgarden.bbs.model.helper.SortPointer;
import com.yhgarden.bbs.model.response.BaseResponse;
import com.yhgarden.bbs.service.api.business.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutorService;

/**
* @author Laijiaxiang
* @since 2019-09-23
*/
@Service
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private UserGroupMapper userGroupMapper;
    
    @Autowired
    private ExecutorService executorService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse createOrUpdate(UserGroup userGroup) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        if(userGroup.getId() == null){
            String uuid = UUID.randomUUID().toString();
            userGroup.setId(uuid);
            userGroupMapper.create(userGroup);
        }else {
            userGroupMapper.update(userGroup);
        }
        response.setData(userGroup);
        return response;
    }

    @Override
    public BaseResponse getById(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        UserGroup userGroup = userGroupMapper.getById(id);
        response.setData(userGroup);
        return response;
    }
    
    @Override
    public BaseResponse getByIdForList(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        UserGroup userGroup = userGroupMapper.getByIdForList(id);
        response.setData(userGroup);
        return response;
    }
    
    
    @Override
    public BaseResponse getByIdMoreDetail(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        UserGroup userGroup = userGroupMapper.getByIdMoreDetail(id);
        response.setData(userGroup);
        return response;
    }


    @Override
    public BaseResponse query(UserGroup userGroup) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        Pager pager = userGroup.getPager();
        SortPointer sortPointer = userGroup.getSortPointer();
        if(sortPointer == null){
            sortPointer = new SortPointer();
        }
        List<UserGroup> userGroupList = userGroupMapper.query(userGroup,sortPointer.getOrder(),sortPointer.getField());
        response.setData(userGroupList);
        return response;
    }


    @Override
    public BaseResponse queryForList(UserGroup userGroup) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        Pager pager = userGroup.getPager();
        SortPointer sortPointer = userGroup.getSortPointer();
        if(sortPointer == null){
            sortPointer = new SortPointer();
        }
        PageHelper.startPage(pager.getCurrentPage(),pager.getPageSize());
        List<UserGroup> userGroupList = userGroupMapper.queryForList(userGroup,sortPointer.getOrder(),sortPointer.getField());
        PageInfo result = new PageInfo<UserGroup>(userGroupList);
        response.setData(result);
        return response;
    }


    @Override
    public BaseResponse queryMoreDetail(UserGroup userGroup) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        Pager pager = userGroup.getPager();
        SortPointer sortPointer = userGroup.getSortPointer();
        if(sortPointer == null){
            sortPointer = new SortPointer();
        }
        PageHelper.startPage(pager.getCurrentPage(),pager.getPageSize());
        List<UserGroup> userGroupList = userGroupMapper.queryMoreDetail(userGroup,sortPointer.getOrder(),sortPointer.getField());
        PageInfo result = new PageInfo<UserGroup>(userGroupList);
        response.setData(result);
        return response;
    }


    @Override
    public BaseResponse delete(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        userGroupMapper.delete(id);
        return response;
    }    

}

